package cn.jsj.cxsys.service.impl;

import org.springframework.stereotype.*;
import cn.jsj.cxsys.service.*;
import org.springframework.beans.factory.annotation.*;
import javax.annotation.*;
import cn.jsj.cxsys.dao.*;
import java.util.*;
import com.github.pagehelper.*;
import org.springframework.transaction.annotation.*;
import cn.jsj.cxsys.pojo.*;

@Service
public class ComputerServiceImpl implements ComputerService
{
    @Autowired
    private UserService userService;
    @Resource
    private StudentDoLab studentDoLab;
    @Resource
    private ComputerFunction computerFunction;
    @Resource
    private LabFunction labFunction;
    @Resource
    private RecordFunction recordFunction;
    @Resource
    private UserFunction userFunction;

    @Override
    public List<Computer> getComputersOfLab(final User user, final int labID, final int pageNum, final int pageSize) {
        if (labID < 1) {
            return null;
        }
        if ("error".equals(this.userService.login(user))) {
            return null;
        }
        final int i = this.studentDoLab.checkStudentHavePrivelageOfLab(labID, user.getUserID());
        if (i == 1) {
            PageHelper.startPage(pageNum, pageSize);
            return this.computerFunction.getComputersOfOneLab(labID);
        }
        return null;
    }

    @Override
    public List<Computer> getAllComputer(final String superAccount, final String password, final int pageNum, final int pageSize) {
        final String inf = this.userService.login(superAccount, password);
        if ("superLader".equals(inf)) {
            PageHelper.startPage(pageNum, pageSize);
            return this.computerFunction.getAllComputers();
        }
        return null;
    }

    @Override
    public String addOneComputer(final User user, final Computer computer) {
        if (computer == null || computer.getLabID() < 1 || computer.getState() != 0 || computer.getPcNum() < 0) {
            return "paramLess";
        }
        final String inf = this.userService.login(user);
        if (!inf.equals("teacher")) {
            return inf;
        }
        final User us = this.labFunction.getLaderOfLabByID(computer.getLabID());
        if (us == null) {
            return "identityError";
        }
        if (us.getUserID() != user.getUserID()) {
            return "identityError";
        }
        final int i = this.computerFunction.insertOneComputer(computer.getPcNum(), computer.getLabID());
        if (i == 1) {
            return "success";
        }
        return "error";
    }

    @Override
    public String addComputersInLab(final User user, final int labID, int computerNumber) {
        if (computerNumber < 1) {
            return "error";
        }
        final String str = this.userService.login(user);
        if (!"teacher".equals(str)) {
            return "identityError";
        }
        final int i = this.labFunction.judgeTeacherIsTheLaderOfLab(user.getUserID(), labID);
        if (i != 1) {
            return "error";
        }
        int maxValue = this.computerFunction.getMaxComputerNumOfLab(labID);
        boolean key = false;
        if (computerNumber + maxValue > 200) {
            computerNumber = 200 - maxValue;
            key = true;
        }
        for (int j = 0; j < computerNumber; ++j) {
            ++maxValue;
            final int k = this.computerFunction.insertOneComputer(maxValue, labID);
            if (k == 0) {
                return "error";
            }
        }
        if (!key) {
            return "success";
        }
        return "successButNumberLimit";
    }

    @Override
    public String removeOneComputer(final User user, final Computer computer) {
        if (computer == null || computer.getPcID() < 1 || computer.getPcNum() < 0) {
            return "paramLess";
        }
        final String inf = this.userService.login(user);
        if (!"teacher".equals(inf)) {
            return "error";
        }
        final User tuser = this.labFunction.getLaderOfLabByID(computer.getLabID());
        if (user.getUserID() != tuser.getUserID()) {
            return "error";
        }
        final int r = this.computerFunction.deleteOneLab(computer.getLabID(), computer.getPcNum());
        if (r == 1) {
            return "success";
        }
        return "error";
    }

    @Transactional
    @Override
    public String setBreakOfComputer(final User user, final int computerID, final String breakinf) {
        if (breakinf == null || breakinf.trim().length() < 1 || breakinf.trim().length() > 200 || computerID < 1) {
            return "paramLess";
        }
        final String inf = this.userService.login(user);
        if ("error".equals(inf)) {
            return "error";
        }
        final Computer computer = this.computerFunction.getOneComputerByID(computerID);
        if (computer.getState() != user.getUserID()) {
            return "error";
        }
        int i = this.computerFunction.setBreak(computer.getPcID(), breakinf.trim());
        if (i != 1) {
            return "error";
        }
        final String time = NowDateFactory.getNowTime();
        i = this.recordFunction.sigoutRecord(user.getUserID(), time, breakinf);
        if (i != 1) {
            return "error";
        }
        i = this.labFunction.labPeopleDecline(computer.getLabID());
        if (i == 1) {
            this.userFunction.deleteOneOnlineRecord(user.getUserID());
            return "success";
        }
        return "error";
    }

    @Transactional
    @Override
    public String setWellOfComputer(User user, final int computerID) {
        if (computerID < 1) {
            return "paramLess";
        }
        final String inf = this.userService.login(user);
        if (!"teacher".equals(inf)) {
            return "error";
        }
        user = this.userService.getUserByAccount(user.getAccount(), user.getPassword());
        final Lab lab = this.computerFunction.getFatherLab(computerID);
        if (lab == null) {
            return "error";
        }
        if (!user.getTeleNumber().equals(lab.getLaderTeleNumber())) {
            return "error";
        }
        final int i = this.computerFunction.repareComputer(computerID);
        if (i == 1) {
            return "success";
        }
        return "error";
    }

    @Transactional
    @Override
    public String signInComputer(final User user, final int computerID) {
        if (computerID < 1) {
            return "paramLess";
        }
        final String inf = this.userService.login(user);
        if (!"student".equals(inf)) {
            return "error";
        }
        final String sta = this.userService.checkStudentState(user);
        if (sta.equals("error") || sta.equals("inLab") || sta.equals("onComputer")) {
            return "YouAreOnline";
        }
        final Computer computer = this.computerFunction.getOneComputerByID(computerID);
        if (computer == null) {
            return "error";
        }
        int i = this.studentDoLab.checkStudentHavePrivelageOfLab(computer.getLabID(), user.getUserID());
        if (i == 0) {
            return "YouNeetToApply";
        }
        i = this.computerFunction.signInComputer(user.getUserID(), computer.getPcID());
        if (i != 1) {
            return "ComputerWorking";
        }
        final String time = NowDateFactory.getNowTime();
        i = this.recordFunction.siginRecord(user.getUserID(), computer.getPcNum(), computer.getLabID(), time);
        if (i != 1) {
            return "error";
        }
        i = this.labFunction.labPeopleAddOne(computer.getLabID());
        if (i == 1) {
            this.userFunction.userAddOnlineComputer(user.getUserID(), computerID);
            return "success";
        }
        return "error";
    }

    @Transactional
    @Override
    public String signOutComputer(final User user, final int computerID, String str) {
        if (computerID < 1) {
            return "paramLess";
        }
        final String inf = this.userService.login(user);
        if (!"student".equals(inf)) {
            return "error";
        }
        final String sta = this.userService.checkStudentState(user);
        if (sta.equals("error") || sta.equals("outline") || sta.equals("inLab")) {
            return "stateError";
        }
        final Computer computer = this.computerFunction.getOneComputerByID(computerID);
        if (computer == null) {
            return "errorComputerID";
        }
        int i = this.computerFunction.signOutComputer(user.getUserID(), computer.getPcID());
        if (i == 0) {
            return "ComputerOfUserError";
        }
        if (str == null || str.trim().length() < 1 || str.trim().length() > 100) {
            str = "\u7528\u6237\u672a\u586b\u5199\u4f7f\u7528\u4fe1\u606f";
        }
        final String time = NowDateFactory.getNowTime();
        i = this.recordFunction.sigoutRecord(user.getUserID(), time, str.trim());
        if (i != 1) {
            return "error";
        }
        i = this.labFunction.labPeopleDecline(computer.getLabID());
        if (i == 1) {
            this.userFunction.deleteOneOnlineRecord(user.getUserID());
            return "success";
        }
        return "error";
    }

    @Override
    public int getNumberOfComputer() {
        return this.computerFunction.countNumberOfComputers();
    }
}
